package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Artisan;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.ArtisanService;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/09 16:24
 */
@RestController
public class ArtisanController {

    @Autowired
    ArtisanService artisanService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询
     *
     * @param page   页码
     * @param size   每页条数
     * @param draw   前台参数，照样返回
     * @param search 搜索，如果无搜索此值为paging
     * @param sort   time，alternative，nosort
     * @param way    desc，asc，noway
     */
    @GetMapping("/artisan/list/{page}/{size}/{draw}/{search}/{sort}/{way}/{isAudit}/{demandCategoryId}")
    public Response list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size,
                         @PathVariable("draw") String draw,
                         @PathVariable("search") String search,
                         @PathVariable("sort") String sort,
                         @PathVariable("way") String way,
                         @PathVariable("isAudit") String isAudit,
                         @PathVariable("demandCategoryId") String demandCategoryId) {
        Response response = new Response();
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = artisanService.selectAllByDel(0, search, sort, way, isAudit,demandCategoryId);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, (int) pageInfo.getTotal(), draw);
            response.setCode(RetCode.SUCCESS);
            response.put("pageList", pageList);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        System.out.println(response);
        return response;
    }

    /**
     * 技工注册
     *
     * @param artisan
     * @return
     */
    @PostMapping("/artisan/save")
    public Response save(HttpServletRequest request,Artisan artisan, MultipartFile headImg) {

        Map<String, Object> map = new HashMap<>();
        Response response = new Response();
        if (headImg != null) {
            String result = fileUploadService.saveImgUpload(request, headImg);
            if (result.equals("formatError")) {
                response.setData(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                artisan.setArtisanHeadImg(result);
                response = artisanService.save(artisan);
            }
        } else {
            response = artisanService.save(artisan);
        }
        return response;

    }

    /**
     * 查询技工详情
     *
     * @param id:技工的id
     * @return
     */
    @GetMapping("/artisan/getArtisanById/{id}")
    public Response getArtisanById(@PathVariable("id") String id) {
        return artisanService.getArtisanById(id);
    }


    /**
     * 通过customerId    ----------查询技工详情---------技工申请页面

     * @return
     */
    @GetMapping("/artisan/getArtisanByCustomerId")
    public Response getArtisanByCustomerId() {
        return artisanService.getArtisanByCustomerId();
    }



    /**
     * 修改技工资料
     *
     * @param request
     * @param artisan
     * @param headImg
     * @return
     */
    @RequestMapping(value = "/artisan/update")
    public Response update(HttpServletRequest request, Artisan artisan, MultipartFile headImg) {
        Map<String, Object> map = new HashMap<>();
        Response response = new Response();
        if (headImg != null) {
            String result = fileUploadService.saveImgUpload(request, headImg);
            if (result.equals("formatError")) {
                response.setData(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                artisan.setArtisanHeadImg(result);
                response = artisanService.update(artisan);
            }
        } else {
            response = artisanService.update(artisan);
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id:技工的id
     * @return
     */
    @DeleteMapping("/artisan/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        return artisanService.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param id:技工的id集合
     * @return
     */
    @DeleteMapping("/artisan/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") String[] id) {
        return artisanService.batchDeleteById(id);
    }

    /**
     * 技工的删除
     *
     * @param id:技工的id
     * @return
     */
    @GetMapping("/artisan/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id") String id,
                                            @PathVariable("status") Integer status) {
        return artisanService.changeStatus(id, status);
    }

    /**
     * 技工的审核
     *
     * @param id:技工的id
     *          isAudit  0:待审核  1:审核通过  -1:驳回
     * @return
     */
    @GetMapping("/artisan/changeIsAudit/{id}/{isAudit}")
    public Response changeIsAudit(@PathVariable("id") String id,
                                 @PathVariable("isAudit") Integer isAudit) {
        return artisanService.changeIsAudit(id, isAudit);
    }

    /**
     * 首页推荐技工
     *
     * @param city：城市
     * @return
     */
    @RequestMapping(value = "/artisan/recommendArtisan", method = RequestMethod.GET)
    public Response recommendArtisan(@RequestParam(value = "city", required = false) String city) {
        return artisanService.recommendArtisan(city);
    }


    /*
             通过customerId  查询技工信息
     */
    @RequestMapping(value = "/artisan/getArtisanByCustomer", method = RequestMethod.GET)
    public Response getArtisanByCustomer(@RequestParam(value = "customerId") String customerId) {
        return artisanService.getArtisanByCustomer(customerId);
    }



}
